package com.machuxin.course.patterns.factory.simple;


import com.machuxin.course.patterns.factory.simple.beans.AppleFruits;
import com.machuxin.course.patterns.factory.simple.beans.BananaFruits;
import com.machuxin.course.patterns.factory.simple.beans.Fruits;

/**
 * Created by Tom.
 */
public class FruitsFactory {

/** 通过名称    */
//    public Fruits create(String name){
//        if("apple".equals(name)){
//            return new AppleFruits();
//        }else if("banana".equals(name)){
//            return new BananaFruits();
//        }else {
//            return null;
//        }
//    }

/** 通过反射    */
//    public Fruits create(String className){
//        try {
//            if (!(null == className || "".equals(className))) {
//                return (Fruits) Class.forName(className).newInstance();
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return null;
//    }

/** 通过类    */
    public Fruits create(Class<? extends Fruits> clazz) {
        try {
            if (null != clazz) {
                return clazz.newInstance();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
